package hhm.android.my

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import hhm.android.base.adapter.HBaseAdapter
import hhm.android.library.utils.GlideUtils
import siau.android.http.model.SystemMessageListModel
import java.text.SimpleDateFormat

class MessageSystemListAdapter(var context: Context, list: ArrayList<SystemMessageListModel>) : HBaseAdapter<MessageSystemListViewHolder, SystemMessageListModel>(list) {
    override fun bindView(parent: ViewGroup, viewType: Int): MessageSystemListViewHolder {
        val v = LayoutInflater.from(context).inflate(R.layout.item_message_system_list, parent, false)
        return MessageSystemListViewHolder(v)
    }

    override fun bindData(holder: MessageSystemListViewHolder, bean: SystemMessageListModel, position: Int) {
        if (position == 0) {
            val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
            holder.time.text = sdf.format(bean.sendTime)
            holder.time.visibility = View.VISIBLE
        } else {
            val lastBean = list[position - 1]
            if (bean.sendTime - lastBean.sendTime <= 1000 * 60) {
                holder.time.visibility = View.GONE
            } else {
                val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
                holder.time.text = sdf.format(bean.sendTime)
                holder.time.visibility = View.VISIBLE
            }
        }
        holder.tv.text = bean.content

        GlideUtils.loadCircleImage(
            context, if (TextUtils.isEmpty(bean.avatar)) {
                ""
            } else {
                bean.avatar
            }, holder.iv, R.mipmap.ic_launcher, R.mipmap.ic_launcher
        )

    }
}